package com.xiaotu.spider.soushi.pageprocessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVSetsPlotModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.TVProcessUtils;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 SouShiPageProcessor
 * @日期 2017年12月29日
 * @作者 王艳龙
 * @功能 捜视网分集梗概数据爬取
 */
@Component("SouShiPageProcessor")
@Scope("prototype")
public class SouShiPageProcessor extends APageProcessor
{
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		return new Request("https://www.tvsou.com/search/"
				+ KeywordEncode.encode(target.getString("tvName"))
				+"?type=item");
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		String url = page.getUrl().toString();
		if (url.contains("https://www.tvsou.com/search"))
			return Constants.PageDataType.SEARCH_LIST;
		else if (url.contains("https://www.tvsou.com/story") && !url.contains("result"))
			return Constants.PageDataType.TV_RECORD;
		else if (url.contains("result"))
			return Constants.PageDataType.TV_PLOTSET;
		else
		return Constants.PageDataType.UNKNOW;
	}
	
	@Override
	public void process(Page page)
	{
		Map<String, Object> tvMap = page.getRequest().getExtras();
		switch (this.getPageDataType(page))
		{
			case Constants.PageDataType.SEARCH_LIST:
				this.searchListProcess(page, tvMap);
				break;
			case Constants.PageDataType.TV_RECORD:
				this.tvRecordProcess(page, tvMap);
				break;
			case Constants.PageDataType.TV_PLOTSET:
				this.plotsetProcess(page, tvMap);
				break;
			default:
				break;
		}
	}
	
	private void searchListProcess(Page page, final Map<String, Object> tvInfoMap)
	{
        List<String> tvnameList = new ArrayList<String>();
        List<String> linkList = new ArrayList<String>();
        List<String> tvTypeList = new ArrayList<String>();
        List<List<String>> actorList = new ArrayList<List<String>>();
        
        //搜索列表
        final List<String> searchListHTML = page.getHtml()
        		.xpath("//div[@class='group-main ov']/div").all();
        if(searchListHTML.isEmpty())
        	throw new SpiderException(
        			SpiderExceptionCode.DataInvalideException);
        for (String htmlStr : searchListHTML) {
        	Html html = new Html(htmlStr);
        	
        	 tvnameList.add(html
                    .xpath("//div[@class='index-l-box-img-info']/div/a[@id='custom-em']/em/text()")
                    .toString());
            linkList.add(html
                    .xpath("//div[@class='index-l-box-img-info']/div/a[@id='custom-em']/@href")
                    .toString());
            String typenames = html.xpath("//div[@class='index-l-box-img l']/a/span/text()").toString();
            if(StringUtils.isNotEmpty(typenames) && typenames.contains(SepratorUtil.SEP_SLASH_EN)){
            	String[] typeName = typenames.split(SepratorUtil.SEP_SLASH_EN);
            	tvTypeList.add(typeName[0].trim());
            }else{
            	tvTypeList.add(typenames);
            }
            List<String> actor = new ArrayList<String>();
            String actors =html.xpath("//div[@class='index-l-box-img-info']/div[@class='font-12 color-9 tag']/text()").toString();
            if(StringUtils.isNotEmpty(actors) && actors.length()>1){
            	actors =actors.substring(5, actors.length());
            	String[] actorsArr =actors.split(SepratorUtil.SEP_SLASH_EN);
            	for (String stringStr : actorsArr) {
            		actor.add(stringStr.trim());
            	}
            }
            	actorList.add(actor);
		}
        
		if (linkList.size() != tvnameList.size()
                || linkList.size() != tvTypeList.size()
                || linkList.size() != actorList.size())
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);
        
        
        TVProcessUtils.addTVFromSearchList(page, tvInfoMap, tvnameList,
        		linkList, tvTypeList, actorList, null,
        		new TVProcessUtils.TVMatcher()
        {
        	@Override
        	protected boolean addTV(Page page,
        			Map<String, Object> tvinfoMap, String name,
        			String tvType, List<String> actorsList,
        			List<String> directorsList, String link, int idx)
        	{
        		if (TVProcessUtils.tvMatched(tvinfoMap, name,
        				TVProcessUtils.getTVType(tvType), actorsList,
        				null))
        		{
        			link =link.replaceAll("show", "story");
        			PageUtils.addTargetRequest(page,link,tvInfoMap, true);
        			return true;
        		}
        		return false;
        	}
        }, null);
	}
	
	private void tvRecordProcess(Page page, Map<String, Object> tvMap)
	{
		//获取所有集数地址
		List<String> dataList =page.getHtml().xpath("//div[@class='zy-source-box']/div[@class='tv-collect']"
				+ "/a[@class='color-3 borer-5 text-c episodes-dd episodes-0 hide']/@href").all();
		if(dataList.isEmpty())
        	throw new SpiderException(
        			SpiderExceptionCode.DataInvalideException);
		for (String links : dataList) {
			PageUtils.addTargetRequest(page,links+"?result",tvMap, true);
		}
	}
	
	private void plotsetProcess(Page page, Map<String, Object> tvMap)
	{
		String dataList =page.getRawText();
		if(dataList.isEmpty())
        	throw new SpiderException(
        			SpiderExceptionCode.DataInvalideException);
		String sets =page.getHtml().xpath("//div[@class='l topics']//p[@class='mt-20']/text()").toString();
		if(sets !=null && sets.contains("【")){
			sets = sets.replaceAll("【", "").replaceAll("】", "");
		}
		String set = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, sets.toString());
		String plots =page.getHtml().xpath("//div[@class='l topics']//pre[@class='font-16 color-3 mt-20 pre-content']").toString();
		if(plots !=null){
			plots = plots.replaceAll("（搜视网原创剧情，小编创作辛苦，未经许可，请勿转载！）", "");
			plots = HtmlUtil.delHTMLTag(plots);
		}
		String plot =sets+SepratorUtil.SEP_SPACE_EN+SepratorUtil.SEP_SPACE_EN+SepratorUtil.SEP_SPACE_EN
				+SepratorUtil.SEP_SPACE_EN+SepratorUtil.SEP_SPACE_EN+plots;
		
		page.putField(TVSetsPlotModel.class.getName(),
				new TVSetsPlotModel(tvMap.get("tvid") + "",
						tvMap.get("tvName") + "", set, plot));
	}
	
	@Override
	protected String getSpiderType()
	{
		return "Sohu";
	}
}
